/***
*
* File: wp_utils.nut
* Auth: Damian Wojdat
* Date: 20.10.2012
* Desc: Mostly functions like string processing, maths, etc.
*
*************************************************************************************************************/

dofile("scripts/wp/utils/wp_utils_player.nut",true);
dofile("scripts/wp/utils/wp_utils_chat.nut",true);
dofile("scripts/wp/utils/wp_utils_logs.nut",true);
dofile("scripts/wp/utils/wp_utils_vehicles.nut",true);

function subString(array, start)
{
	local string = "";
	foreach(i, val in array)
	{
		if(i == start) string += array[i];
		else if(i >= start) string += " "+array[i];
	}
	return string;
}

function proxDetector(radi, playerid, string, col1, col2, col3, col4, col5)
{
	if(isPlayerConnected(playerid))
	{
		local pos;
		local oldPos;
		local tempPosX;
		local tempPosY;
		local tempPosZ;
		
		oldPos = getPlayerCoordinates(playerid);
		
		foreach(i, playername in getPlayers())
		{
			if(isPlayerConnected(i))
			{
				pos = getPlayerCoordinates(i);
				
				tempPosX = oldPos[0] - pos[0];
				tempPosY = oldPos[1] - pos[1];
				tempPosZ = oldPos[2] - pos[2];
				
				if (((tempPosX < (radi/16).tofloat()) && (tempPosX > (-radi/16).tofloat())) && ((tempPosY < (radi/16).tofloat()) && (tempPosY > (-radi/16).tofloat())) && ((tempPosZ < (radi/16).tofloat()) && (tempPosZ > (-radi/16).tofloat())))
				{
					sendPlayerMessage(i.tointeger(), string.tostring(), col1.tointeger());
				}
				else if (((tempPosX < (radi/8).tofloat()) && (tempPosX > (-radi/8).tofloat())) && ((tempPosY < (radi/8).tofloat()) && (tempPosY > (-radi/8).tofloat())) && ((tempPosZ < (radi/8).tofloat()) && (tempPosZ > (-radi/8).tofloat())))
				{
					sendPlayerMessage(i.tointeger(), string.tostring(), col2.tointeger());
				}
				else if (((tempPosX < (radi/4).tofloat()) && (tempPosX > (-radi/4).tofloat())) && ((tempPosY < (radi/4).tofloat()) && (tempPosY > (-radi/4).tofloat())) && ((tempPosZ < (radi/4).tofloat()) && (tempPosZ > (-radi/4).tofloat())))
				{
					sendPlayerMessage(i.tointeger(), string.tostring(), col3.tointeger());
				}
				else if (((tempPosX < (radi/2).tofloat()) && (tempPosX > (-radi/2).tofloat())) && ((tempPosY < (radi/2).tofloat()) && (tempPosY > (-radi/2).tofloat())) && ((tempPosZ < (radi/2).tofloat()) && (tempPosZ > (-radi/2).tofloat())))
				{
					sendPlayerMessage(i.tointeger(), string.tostring(), col4.tointeger());
				}
				else if (((tempPosX < radi.tofloat()) && (tempPosX > -radi.tofloat())) && ((tempPosY < radi.tofloat()) && (tempPosY > -radi.tofloat())) && ((tempPosZ < radi.tofloat()) && (tempPosZ > -radi.tofloat())))
				{
					sendPlayerMessage(i.tointeger(), string.tostring(), col5.tointeger());
				}
			}
		}
		return 1;
	}
	return 1;
}

function proxDetectorS(radi, playerid, targetid)
{
    if(isPlayerConnected(playerid) && isPlayerConnected(targetid))
	{
		local posx;
		local posy;
		local posz;
		
		local oldposx;
		local oldposy;
		local oldposz;
		
		local tempposx;
		local tempposy;
		local tempposz;
		
		getPlayerCoordinates(playerid, oldposx, oldposy, oldposz);
		getPlayerCoordinates(targetid, posx, posy, posz);
		tempposx = (oldposx -posx);
		tempposy = (oldposy -posy);
		tempposz = (oldposz -posz);
		if (((tempposx < radi) && (tempposx > -radi)) && ((tempposy < radi) && (tempposy > -radi)) && ((tempposz < radi) && (tempposz > -radi)))
		{
			return 1;
		}
	}
	return 0;
}

function playerToPoint(radi, playerid, x, y, z)
{
	local oldpos = getPlayerCoordinates(playerid);
	local tempposx = (oldpos[0] -x);
	local tempposy = (oldpos[1] -y);
	local tempposz = (oldpos[2] -z);
	if (((tempposx < radi) && (tempposx > -radi)) && ((tempposy < radi) && (tempposy > -radi)) && ((tempposz < radi) && (tempposz > -radi)))
		return 1;
	return 0;
}

function isInteger(number) 
{
	try
	{
		number.tointeger();
	}
	catch(e)
	{
		return false;
	}
	return true;
}

// function random(min, max)
// {
   // local r = rand();
   // srand(getTickCount() * r);
   // return (r % ((max + 1) - min)) + min;
// }

function factionRankName(fID, rID)
{
	switch(rID)
	{
		case 1: return fData[fID].rank1;
		case 2: return fData[fID].rank2;
		case 3: return fData[fID].rank3;
		case 4: return fData[fID].rank4;
		case 5: return fData[fID].rank5;
		case 6: return fData[fID].rank6;
		case 7: return fData[fID].rank7;
		case 8: return fData[fID].rank8;
		case 9: return fData[fID].rank9;
		case 10: return fData[fID].rank10;
		case 11: return fData[fID].rank11;
		case 12: return fData[fID].rank12;
		case 13: return fData[fID].rank13;
		case 14: return fData[fID].rank14;
		case 15: return fData[fID].rank15;
	}
}

function isFileExist(string)
{
	if(typeof(string) != "string") return 0;
	try
	{
		local getFile = file(string, "r");
	}
	catch(e)
	{
		return 0;
	}
	return 1;
}

function random(min,max)
{
	return min+((max-min+1)*rand()/RAND_MAX).tointeger();
} 

function selectRandomRound()
{	
	gSelectedRound = random(0, gRoundCount-1);
	gRoundTimeLeft = gDefaultRoundTime;
	gRoundStartTime = gDefaultRoundStartTime;
	foreach(i, playername in getPlayers())
	{
		gJustDied[i] = 1;
		pData[i].timeToRespawn = gRoundStartTime+5;
		setPlayerSpawn(i);
	}
}